#include "gofgame/dungeonkeeper/ui/ChoiceUnitDialog.h"
#include "gofgame/common/ui/Image.h"

using namespace gofgame::dungeonkeeper::ui;

ChoiceUnitDialog::ChoiceUnitDialog() 
	: MessageViewDialog(MessageDialogType_OKCancel) {
	CCSize size = CCDirector::sharedDirector()->getWinSize();
	this->setWidth(size.width * 3 / 4);
	this->setHeight(size.height * 3 / 4);
}

bool ChoiceUnitDialog::init() {
	if (!MessageViewDialog::init()) {
		return false;
	}
	this->setTitle("Please choose the unit");
	return true;
}

Ref<Component> ChoiceUnitDialog::createMainComponent() {
	static const char *headIcon[] = {
		"U_darkElf.png",
		"U_darkKnight.png",
		"U_darkLady.png",
		"U_darkMage.png",
		"U_dwarf.png",
		"U_liandao.png",
		"U_skull.png",
	};
	Ref<ChoiceView> choiceView;
	choiceView.initialize();
	choiceView->init();
	Ref<Template> sourceItemTemplate;
	sourceItemTemplate.initialize(new UnitTemplate());
	choiceView->setSourceItemTemplate(sourceItemTemplate);
	list<Ref<CCObject>> dataSource;
	for (int i = 0; i < sizeof(headIcon) / sizeof(headIcon[0]); i++) {
		Ref<CCString> dataItem;
		dataItem.initialize();
		dataItem->initWithFormat("Unit/%s", headIcon[i]);
		dataSource.push_back(dataItem);
	}
	choiceView->setItemSize(CCSize(130, 130));
	choiceView->setDataSource(dataSource);
	return choiceView;
}

Ref<Component> ChoiceUnitDialog::UnitTemplate::createComponent(CCObject *dataItem) {
	Ref<Image> image;
	image.initialize();
	image->init();
	image->setResourceName(static_cast<CCString*>(dataItem)->getCString());
	image->setDisplayMode(ImageDisplayMode_Stretch);
	return image;
}

void ChoiceUnitDialog::onDialogClosed(DialogClosedEventArgs &e){
	if(DialogResult_OK == e.getDialogResult()) {
		
	}
}